package com.lovestacks.tj.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.lovestacks.tj.bean.*;
import com.lovestacks.tj.dao.NewMediaOperationDao;
import com.lovestacks.tj.dao.PersonInfoDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Date;
import java.util.*;

import static javax.swing.UIManager.get;


@Service
public class NewMediaOperationService {

    private static Map<String,String> KindMap = new HashMap<>();

    static {
        KindMap.put("fp","第一责任人");
        KindMap.put("tp","三审人员");
        KindMap.put("train","新媒体培训");
        KindMap.put("scheme","专题策划");
        KindMap.put("award","峰会获奖");
        KindMap.put("influence","新媒体影响力排行");
        KindMap.put("hightlight","承办新媒体亮点工作");
        KindMap.put("acticity","举办风采、直播活动");
        KindMap.put("studio","建设新媒体工作室");
    }

    @Autowired
    private NewMediaOperationDao newMediaOperationDao;
    @Autowired
    private PersonInfoDao personInfoDao;

    @Transactional
    public void genNewMedia(String kind, String openId, String pname, String pphone, String work, String pdepart, String pmenu, String pic, String explain, int code, String theme, Date date, String type, String sname, int validate) {

        //根据userId获取部门信息
        PersonInfomation personInfo = personInfoDao.getPersonInfo(openId);

        //构建新媒体对象
        NewMediaOperation newMediaOperation = new NewMediaOperation();
        newMediaOperation.setID(validate);
        newMediaOperation.setOpenId(openId);
        newMediaOperation.setKind(kind);
        newMediaOperation.setDepartment(personInfo.getDepart());
        newMediaOperation.setEnterprises(personInfo.getMenu());
        newMediaOperation.setStatus(code);


        //构建第一责任人对象
        NewMediaAllKindInfo newMediaAllKindInfo = new NewMediaAllKindInfo();
        newMediaAllKindInfo.setPname(pname);
        newMediaAllKindInfo.setPphone(pphone);
        newMediaAllKindInfo.setWork(work);
        newMediaAllKindInfo.setPdepart(pdepart);
        newMediaAllKindInfo.setPmenu(pmenu);
        newMediaAllKindInfo.setPic(pic);
        newMediaAllKindInfo.setExplain(explain);
        newMediaAllKindInfo.setTheme(theme);
        newMediaAllKindInfo.setDate(date);
        newMediaAllKindInfo.setType(type);
        newMediaAllKindInfo.setSname(sname);

        newMediaOperation.setKindInfo(JSON.toJSONString(newMediaAllKindInfo));
        System.out.println("信息如下:" + JSON.toJSONString(newMediaOperation));
        if (validate == 0) {
            newMediaOperationDao.insertNewMediaOperation(newMediaOperation);
            System.out.println(kind + "插入成功");
        } else {
            newMediaOperationDao.updateNewMediaOperation(newMediaOperation);
            System.out.println(kind + "修改成功");
        }
    }

    public Map<String,Object> getNewMedia(String kind, int validate, String openId) {
        Map<String,Object> result = new HashMap<>();
        NewMediaAllKindInfo newMediaOperation1;
        NewMediaOperation person;
        if (validate == 0) {
            //根据userId获取部门信息
            PersonInfomation personInfo = personInfoDao.getPersonInfo(openId);
            //构建新媒体对象
            NewMediaOperation newMediaOperation = new NewMediaOperation();
//        newMediaOperation.setKind(KIND);
            newMediaOperation.setKind(kind);
            newMediaOperation.setDepartment(personInfo.getDepart());
            newMediaOperation.setEnterprises(personInfo.getMenu());
            List<NewMediaOperation> tmpPerson = newMediaOperationDao.getNewMediaOperation(newMediaOperation);
            if (tmpPerson.size() == 0){
                return null;
            }
            person = tmpPerson.get(0);

        } else {
            person = newMediaOperationDao.getNewMediaOperationByID(validate);
        }

        newMediaOperation1 = JSON.parseObject(person.getKindInfo(), new TypeReference<NewMediaAllKindInfo>() {});
        String jsonstr1 = JSON.toJSONString(newMediaOperation1);
        String jsonstr2 = JSON.toJSONString(person);
        Map<String, Object> newMidie = JSON.parseObject(jsonstr1);
        Map<String, Object> personDetail = JSON.parseObject(jsonstr2);
        List<String> picurls = JSONObject.parseArray(newMediaOperation1.getPic(), String.class);
        personDetail.put("pic",picurls);
        result.putAll(newMidie);
        result.putAll(personDetail);
        return result;

    }


    public Map<String,Object> getScore(String kind, int validate, String openId) {
        Map<String,Object> result = new HashMap<>();
        ScoreAllKindInfo newMediaOperation1;
        NewMediaOperation person;
        if (validate == 0) {
            //根据userId获取部门信息
            PersonInfomation personInfo = personInfoDao.getPersonInfo(openId);
            //构建新媒体对象
            NewMediaOperation newMediaOperation = new NewMediaOperation();
//        newMediaOperation.setKind(KIND);
            newMediaOperation.setKind(kind);
            newMediaOperation.setDepartment(personInfo.getDepart());
            newMediaOperation.setEnterprises(personInfo.getMenu());
            newMediaOperation.setEnterprises(personInfo.getMenu());
            List<NewMediaOperation> tmpPerson = newMediaOperationDao.getNewMediaOperation(newMediaOperation);
            if (tmpPerson.size() == 0){
                return null;
            }
            person = tmpPerson.get(0);
        } else {
            person = newMediaOperationDao.getNewMediaOperationByID(validate);
        }
        newMediaOperation1 = JSON.parseObject(person.getKindInfo(), new TypeReference<ScoreAllKindInfo>() {
        });
        String jsonstr1 = JSON.toJSONString(newMediaOperation1);
        String jsonstr2 = JSON.toJSONString(person);
        Map<String, Object> newMidie = JSON.parseObject(jsonstr1);
        Map<String, Object> personDetail = JSON.parseObject(jsonstr2);
        List<String> picurls = JSONObject.parseArray(newMediaOperation1.getPic(), String.class);
        personDetail.put("pic",picurls);
        result.putAll(newMidie);
        result.putAll(personDetail);
        return result;

    }


    public void genScore(String kind, String openId, int validate, String fname, String zname, Date fdate, String author, String pic, String explain, int code, int rank, String mediaList) {
        //根据userId获取部门信息
        PersonInfomation personInfo = personInfoDao.getPersonInfo(openId);

        //构建新媒体对象
        NewMediaOperation newMediaOperation = new NewMediaOperation();
        newMediaOperation.setID(validate);
        newMediaOperation.setOpenId(openId);
        newMediaOperation.setKind(kind);
        newMediaOperation.setDepartment(personInfo.getDepart());
        newMediaOperation.setEnterprises(personInfo.getMenu());
        newMediaOperation.setStatus(code);


        //构建第一责任人对象
        ScoreAllKindInfo scoreAllKindInfo = new ScoreAllKindInfo();
        scoreAllKindInfo.setFname(fname);
        scoreAllKindInfo.setZname(zname);
        scoreAllKindInfo.setFdate(fdate);
        scoreAllKindInfo.setAuthor(author);
        scoreAllKindInfo.setPic(pic);
        scoreAllKindInfo.setExplain(explain);
        scoreAllKindInfo.setRank(rank);
        scoreAllKindInfo.setMediaList(mediaList);

        newMediaOperation.setKindInfo(JSON.toJSONString(scoreAllKindInfo));
        System.out.println("信息如下:" + JSON.toJSONString(newMediaOperation));
        if (validate == 0) {
            newMediaOperationDao.insertNewMediaOperation(newMediaOperation);
            System.out.println(kind + "插入成功");
        } else {
            newMediaOperationDao.updateNewMediaOperation(newMediaOperation);
            System.out.println(kind + "修改成功");
        }
    }


    public List<Map<String, Object>> getList(String kind, String openId) {
        List<Map<String, Object>> list = new ArrayList<>();
        PersonInfomation personInfo = personInfoDao.getPersonInfo(openId);
        //构建新媒体对象
        NewMediaOperation newMediaOperation = new NewMediaOperation();
//        newMediaOperation.setKind(KIND);
        newMediaOperation.setKind(kind);
        newMediaOperation.setDepartment(personInfo.getDepart());
        newMediaOperation.setEnterprises(personInfo.getMenu());
        List<NewMediaOperation> persons = newMediaOperationDao.getNewMediaOperation(newMediaOperation);
        for (NewMediaOperation person : persons) {
            Map<String, Object> map = new HashMap<>();
            NewMediaAllKindInfo newMediaOperation1 = JSON.parseObject(person.getKindInfo(), new TypeReference<NewMediaAllKindInfo>() {
            });
            map.put("validate", person.getID());
            map.put("theme", newMediaOperation1.getTheme());
            map.put("date", newMediaOperation1.getDate());
            list.add(map);
        }
        return list;
    }

    public List<Map<String, Object>> getListScore(String kind, String openId) {
        List<Map<String, Object>> list = new ArrayList<>();
        PersonInfomation personInfo = personInfoDao.getPersonInfo(openId);
        //构建新媒体对象
        NewMediaOperation newMediaOperation = new NewMediaOperation();
//        newMediaOperation.setKind(KIND);
        newMediaOperation.setKind(kind);
        newMediaOperation.setDepartment(personInfo.getDepart());
        newMediaOperation.setEnterprises(personInfo.getMenu());
        List<NewMediaOperation> persons = newMediaOperationDao.getNewMediaOperation(newMediaOperation);
        for (NewMediaOperation person : persons) {
            Map<String, Object> map = new HashMap<>();
            ScoreAllKindInfo scoreAllKindInfo = JSON.parseObject(person.getKindInfo(), new TypeReference<ScoreAllKindInfo>() {
            });
            map.put("validate", person.getID());
            map.put("theme", scoreAllKindInfo.getFname());
            map.put("date", scoreAllKindInfo.getFdate());
            list.add(map);
        }
        return list;
    }

    public List<Map<String,Object>> scanList(String openId){
        List<Map<String, Object>> list = new ArrayList<>();
        PersonInfomation personInfo = personInfoDao.getPersonInfo(openId);
        //构建新媒体对象
        NewMediaOperation newMediaOperation = new NewMediaOperation();
        newMediaOperation.setDepartment(personInfo.getDepart());
        newMediaOperation.setEnterprises(personInfo.getMenu());
        List<NewMediaOperation> persons = newMediaOperationDao.getScanList(newMediaOperation);
        String[] medias = {"fp","tp","train","scheme"};
        List<String> mediasList= Arrays.asList(medias);
        for (NewMediaOperation person : persons) {
            Map<String, Object> map = new HashMap<>();
            map.put("code", person.getStatus());
            map.put("kind", person.getKind());
            map.put("name", KindMap.get(person.getKind()));
            map.put("date", person.getLastUpdateDate());
            if (mediasList.contains(person.getKind())) {
                NewMediaAllKindInfo newMediaOperation1 = JSON.parseObject(person.getKindInfo(), new TypeReference<NewMediaAllKindInfo>() {
                });
                map.put("validate", person.getID());
                map.put("theme", newMediaOperation1.getTheme());
                List<String> listTmp = JSONObject.parseArray(newMediaOperation1.getPic(), String.class);
//                List<String> listTmp = Arrays.asList(newMediaOperation1.getPic().split(","));
                String  imageUrl = "";
                if (listTmp != null && listTmp.size() != 0){
                    imageUrl = listTmp.get(0);
                }
                map.put("imageUrl", imageUrl);
                list.add(map);
            }else {
                ScoreAllKindInfo scoreAllKindInfo = JSON.parseObject(person.getKindInfo(), new TypeReference<ScoreAllKindInfo>() {
                });
                map.put("validate", person.getID());
                map.put("theme", scoreAllKindInfo.getFname());
                List<String> listTmp = JSONObject.parseArray(scoreAllKindInfo.getPic(), String.class);
//                List<String> listTmp = Arrays.asList(scoreAllKindInfo.getPic().split(","));
                String  imageUrl = "";
                if (listTmp != null && listTmp.size() != 0){
                    imageUrl = listTmp.get(0);
                }
                map.put("imageUrl", imageUrl);
                list.add(map);
            }
        }
        return list;
    }


    public Map<String,Object> getMeidaShow(int validate){
        Map<String,Object> map = new HashMap<>();
        NewMediaOperation person = newMediaOperationDao.getNewMediaOperationByID(validate);
        JSONObject jsonObject = JSON.parseObject(person.getKindInfo());
        Map<String, Object> itemMap = JSONObject.toJavaObject(jsonObject,Map.class);
        Map<String,Object> introduce = new HashMap<>();
        if (itemMap.get("pic") != null){
            List<String> picurls = JSONObject.parseArray(itemMap.get("pic").toString(), String.class);
            String videourl = "";
            for (String str:picurls){
                if (str.endsWith(".mp4")){
                    videourl = str;
                    break;
                }
            }
            map.put("videourl", videourl);
            map.put("imageurls",picurls);
        }


        String nameStr = "";
        if (itemMap.get("theme") != null){
            nameStr = itemMap.get("theme").toString();
        }else if (itemMap.get("fname") != null){
            nameStr = itemMap.get("fname").toString();
        }

        map.put("name", nameStr);
        map.put("time", person.getCreatDate());
        map.put("depart",person.getDepartment());
        map.put("content",itemMap.get("explain"));
        return map;
    }

}
